// 余额
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yue: "",
    paykind:[],
    listNum: 10,
    //是否再请求
    req: false,
    // 没有更多数据了
    nomore: false,
    //分页
    page: 1,
    //选中的金额的index
    index: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var yue = wx.getStorageSync("user").wallet;
    this.setData({
      yue: yue
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.productList(1)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getUserInfoFun();
    var yue = this.data.yue;
    yue = Number(yue).toFixed(2);
    this.setData({
      yue: yue
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 选择充值金额
   */
  chosePay: function(e){
    console.log(e)
    var index = e.currentTarget.dataset.index;
    var paykind = this.data.paykind;
    paykind.map(function(val) {
      console.log(val);
      val.checked =  false;
    })
    paykind[index].checked = true;
    this.setData({
      paykind: paykind,
      index: index
    })
  },
  /**
   * 充值
   */
  payment: function(){
    var that = this;
    var req = this.data.req;
    var u_id = wx.getStorageSync("user").u_id;
    var index = this.data.index;
    index = index ? index: 0;
    console.log(index);
    var paykind = this.data.paykind;
    var product_id = paykind[index].id;
    var params = {
      u_id: u_id,
      product_id: product_id
    }
    if(req == false) {
      app.getRequest("reserve/uptopay/wxPay",params,function(e){
        if(e.code == 200) {
          var data = e.data;
          console.log(data);
          if (typeof data != 'object') data = JSON.parse(data);
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success: function(res){
              wx.showToast({
                title: "充值成功",
              })
              var userInfo = wx.getStorageSync("user");
              var params = {
                openid: userInfo.openid,
                nickName: userInfo.nickName,
                avatarUrl: userInfo.avatarUrl,
                gender: userInfo.gender,
                phone: userInfo.phone
                // p_id: userInfo.p_id
              }
              app.getRequest("api/login/userlogin", params, function (rres) {
                if (rres.code == 200) {
                  wx.setStorageSync("u_id", rres.data.u_id);
                  wx.setStorageSync("user", rres.data);
                  wx.redirectTo({
                    url: '/mine/yue/yue',
                  })
                } else {
                  wx.showToast({
                    title: rres.message,
                    icon: "none"
                  })
                }
              });
            },
            fail: function(res){
              console.log(res);
              wx.showModal({
                title: '',
                content: '支付失败',
                showCancel: false
              })
            }
          })
        } else {
          wx.showToast({
            title: e.message,
          })
        }
        that.setData({
          req: false
        })
      })
    }
  },
  /**
   * 请求列表
   */
  productList: function(page){
    var listNum = this.data.listNum;
    var params = {
      pagenum: page,
      listNum: listNum
    }
    var req = this.data.req;
    var that = this;
    if(req == false) {
      app.getRequest("reserve/onecardproduct/selectOneProduct",params,function(e){
        if(e.code == 200) {
          var paykind = that.data.paykind;
          var length = e.data.list.length;
          if(length > 0) {
            e.data.list.map(function(val){
              val.checked = false;
              paykind.push(val);
            })
            if(page == 1) {
              paykind[0].checked = true;
            }
            that.setData({
              paykind: paykind,
              req: false
            })
          }else{
            that.setData({
              nomore: true
            })
          }
          
        }
      })
    }
  },
  /**
   * 触底加载
   */
  more: function(){
    var page = this.data.page;
    page = Number(page) + 1;
    this.productList(page);
  }
})